package cn.swing.ushare.comment.webapi.controller;

import cn.swing.ushare.comment.service.ICommentService;
import cn.swing.ushare.common.web.JsonResult;
import cn.swing.ushare.common.web.State;
import cn.swing.ushare.pojo.dto.CommentAddNewDTO;
import cn.swing.ushare.pojo.vo.ArticleCommentsVO;
import cn.swing.ushare.pojo.vo.CommentListVO;
import cn.swing.ushare.pojo.vo.CommentMessageVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/comments", produces = "application/json;charset=utf-8")
public class CommentController {
    @Autowired
    private ICommentService service;

    @PostMapping("/postComment")
    public JsonResult<Void> insertComment(@RequestBody CommentAddNewDTO comment) {
        service.insertComment(comment);
        return JsonResult.ok();
    }

    @GetMapping("/update-by-ids")
    public JsonResult<Void> updateByIds(Integer[] ids) {
        service.updateByIds(ids);
        return JsonResult.ok();
    }

    @GetMapping("/delete-by-ids")
    @PreAuthorize("hasAnyAuthority('留言删除')")
    public JsonResult<Void> deleteByIds(Integer[] ids) {
        service.deleteByIds(ids);
        return JsonResult.ok();
    }


    @GetMapping("/list")
    public JsonResult<List<CommentListVO>> list(String param) {
        List<CommentListVO> list = service.list(param);
        return JsonResult.ok(list);
    }

    @GetMapping("/getArticleComment")
    public JsonResult<List<ArticleCommentsVO>> getCommentListByArticleId(Integer id) {
        List<ArticleCommentsVO> list = service.getCommentListByArticleId(id);
        return JsonResult.ok(list);
    }

    @GetMapping("/getArticleChildComment")
    public JsonResult<List<ArticleCommentsVO>> getChildCommentListByArticleId(Integer id) {
        List<ArticleCommentsVO> list = service.getChildCommentListByArticleId(id);
        return JsonResult.ok(list);
    }

    @PostMapping("/listCommentMessages")
    public JsonResult<List<CommentMessageVO>> listCommentMessages(@Param("id") Integer id) {
        List<CommentMessageVO> list = service.listCommentMessages(id);
        return JsonResult.ok(list);
    }

    @PostMapping("/getUnreadMessagesCounts")
    public JsonResult<Integer> getUnreadMessagesCounts(Integer id){
        Integer result = service.getUnreadMessagesCounts(id);
        return JsonResult.ok(result);
    }

    @PostMapping("/markIsRead")
    public JsonResult<Integer> markIsRead(Integer id){
        Integer result = service.markIsRead(id);
        return JsonResult.ok(result);
    }


}
